Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online DDL: revert considerations for migrations with foreign key constraints #14368

Merged
merged 10 commits into from
Nov 7, 2023

Conversation

shlomi-noach
Copy link
Contributor

Description

This PR maps what kind of migrations, that deal with foreign keys, have revertibility risks.

As an obvious example: imagine DROPping a FK child table. Then, as the parent has no children, you DELETE all rows from the parent. Then, you revert the DROP. The child table re-appears with rows that have no compatible rows in the parent.

This is but one example and this PR investigates the potential of other types of migrations to cause FK violation.

It should be noteworthy that FK violation in MySQL is generally in the user's hands. It is always possible, in MySQL, to SET FOREIGN_KEY_CHECKS=0 and introduce violations. The server does not blow up, it's just that enforcement is suspended.

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on the CI
  • Documentation was added or is not required

Deployment Notes

@shlomi-noach shlomi-noach added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Online DDL Online DDL (vitess/native/gh-ost/pt-osc) labels Oct 26, 2023
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 26, 2023

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Oct 26, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Oct 26, 2023
@shlomi-noach shlomi-noach removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request labels Oct 26, 2023
@shlomi-noach
Copy link
Contributor Author

shlomi-noach commented Oct 29, 2023

Up to 7cee6d3, we deal with two situations that are actually manifestations of the same concept: dropping a foreign key.

When you drop a foreign key, you drop a constraint. When a constraint is dropped, your data becomes less constrained, and it is then possible for the data to take forms it could not, before. This is why reverting the drop of a foreign key is a problem: you reintroduce a constraint, and the data may already disagree with the constraint.

Moving on:

  • Adding a FK constraint should not be an issue for the immediate revert. Of course, if you revert-the-revert then you're back in the "re-introduce a FK" problem. So in general, we don't need to worry about a CREATE TABLE.
  • In MySQL, there's no ALTERing a constraint. A constraint can be created or dropped. So we don't need to sorry about modifying a constraint (ALTER CONSTRAINT ... ENFORCE is outside the scope of this discussion).
    Any change to a foreign key definition, e.g. expanding from one column covered to two columns covered or vice versa, is interpreted as a DROP CONSTRAINT + ADD CONSTRAINT. Which means we take care of its revertibility by sheer use of DROP CONSTRAINT as per above.
  • In MySQL, it is impossible to change the data type of a column covered by a foreign key (!). Vitess might actually want to offer this functionality in the long term, and in this case the revert can be impacted by the expansion of the data type scope (e.g. int to bigint). The impact for foreign keys exists, but the scenario is already covered outside the scope of foreign keys.
  • This leaves us with "normal" ALTER TABLE statements. Can there be a scenario where an ALTER TABLE does not add/drop foreign keys, does not change data type, and still has revertibility issues? What could be such scenarios?
    • An immediate suspect is a change to GENERATED column, but thankfully MySQL's foreign keys do not allow use of GENERATED columns neither in parent nor child.
    • There can be an issue if a FK covered column is impacted by a change to a CHECK constraint. This is actually outside the scope of FK. This is really complex because the CHECK constraint expression may be complex and we cannot anticipate exactly how it might limit/expand the possible values.

More thinking to come.

@shlomi-noach
Copy link
Contributor Author

shlomi-noach commented Oct 29, 2023

Noteworthy: at this time Vitess does not prevent the scenario where a FK parent table is DROPped. That is, it allows it. As reminder, in Online DDL tables aren't actually immediately dropped.

First, they are RENAMEd. Children's FK will migrate with the renamed table. What happens from that point on depends on whether this MySQL patch is used or not. If used, then then effective immediately these FK constraints are ignored by InnoDB.
Then, at some point later on, the table is actually dropped. Again, with said patch, children tables still ignore that fact and continue to live happily. But the schema becomes inconsistent, and a logical backup+restore will fail.

So at this time this is not being handled, and we assume that a child table gets dropped (or its foreign key constraints are dropped), before the parent table is dropped.

This is one of the reasons why the DDL strategy flag that allows foreign keys starts with "--unsafe-".

@shlomi-noach shlomi-noach changed the title WIP: Online DDL: revert considerations for migrations with foreign key constraints Online DDL: revert considerations for migrations with foreign key constraints Nov 2, 2023
@shlomi-noach shlomi-noach marked this pull request as ready for review November 2, 2023 09:08
@shlomi-noach
Copy link
Contributor Author

At this time I'm only able to see risks with all things removing-a-foreign-key. To that effect, I'm making this PR ready to review. We can iterate in the future in case we meet new scenarios.

@shlomi-noach shlomi-noach removed the NeedsWebsiteDocsUpdate What it says label Nov 2, 2023
@shlomi-noach shlomi-noach requested a review from a team November 2, 2023 09:08
Copy link
Contributor

@notfelineit notfelineit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a great read! Thank you 🙏

@shlomi-noach shlomi-noach merged commit 3aead9e into vitessio:main Nov 7, 2023
118 of 119 checks passed
@shlomi-noach shlomi-noach deleted the onlineddl-fk-revertible-notes branch November 7, 2023 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Online DDL Online DDL (vitess/native/gh-ost/pt-osc) Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants